# 第一问，循环计数

# for循环的简单计数
dict1 = {"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6, "g": 7}
i1 = 0
for e in dict1:
    i1 += 1
    print(e)
print(f"final dict number:{i1}")
print("——————————————————————————————————————————————————")

# while循环-输出30以内的奇数
j = 0
while j < 30:
    if j % 2 == 1:
        print(int(j))
    j += 1
print("——————————————————————————————————————————————————")

# 第二问
# 100以内斐波那契函数-for
a = 0
b = 1
c = 0
print(a)
print(b)
for i in range(2, 30):
    c = a + b
    a, b = b, c;
    if c < 100:
        print(c)
    else:
        break
print("——————————————————————————————————————————————————")

# 100以内斐波那契函数-while
d = 0
e = 1
f = 0
print(d)
print(e)
while 0 < f < 100:
    print(f)
    f = d + e
    d = e
    e = f
print("——————————————————————————————————————————————————")


# 第三问，自定义异常paramserror

class ParamsError(Exception):
    pass


print('''本计算器可进行以下运算
加法：运算代码'+'
减法：运算代码'-'
乘法：运算代码'*'
除法：运算代码'/'
整除：运算代码'//'
取余：运算代码'%'
开方：运算代码'1/**'
''')
try:
    a = int(input("请输入第一个计算数值："))
    b = input("请输入运算符号：")
    c = int(input("请输入第二个计算数值："))
except ValueError:
    raise ParamsError("请检查输入格式")
finally:
    print("end")


def add(a, c):
    return a + c


def sub(a, c):
    return a - c


def mul(a, c):
    return a * c


def div(a, c):
    try:
        return a / c
    except ZeroDivisionError:
        raise ParamsError("除数不可以为0！")
    finally:
        print("end")


def ediv(a, c):
    try:
        return a // c
    except ZeroDivisionError:
        raise ParamsError("除数不可以为0！")
    finally:
        print("end")


def rem(a, c):
    try:
        return a % c
    except ZeroDivisionError:
        raise ParamsError("取余数不可以为0！")
    finally:
        print("end")


def nthr(a, c):
    try:
        return a ** (1 / c)
    except ZeroDivisionError:
        raise ParamsError("开方数不可以为0！")
    finally:
        print("end")


if b == '+':
    print("计算结果：", add(a, c));
elif b == '-':
    print("计算结果：", sub(a, c));
elif b == '*':
    print("计算结果：", mul(a, c));
elif b == '/':
    print("计算结果：", div(a, c));
elif b == '//':
    print("计算结果：", ediv(a, c));
elif b == '%':
    print("计算结果：", rem(a, c));
elif b == '1/**':
    print("计算结果：", nthr(a, c));
else:
    print(str("无法计算"))
